package svc

import (
	"goadmin-huasen-portal/cache"
	"goadmin-huasen-portal/middleware"
	"goadmin-huasen-portal/util"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"github.com/spf13/viper"
	"gorm.io/gorm"
)

type ServiceContext struct {
	//Config config.Config
	Orm    *gorm.DB
	Redis  *redis.Client
	Logger *util.Logger

	// model
	UserInfoMiddleware  gin.HandlerFunc
	RateLimitMiddleware gin.HandlerFunc
}

func NewServiceContext() *ServiceContext {
	cache.NewMysql(&cache.Config{
		DSN:         viper.GetString("MySQL.DSN"),
		Active:      viper.GetInt("MYSQL.Active"),
		IDle:        viper.GetInt("MYSQL.IDle"),
		IDleTimeout: viper.GetInt64("MYSQL.IDleTimeout"),
	}, viper.GetString("Env"))

	cache.NewRedis(&cache.RedisConfig{
		Host: viper.GetString("Redis.Host"),
		Pass: viper.GetString("Redis.Pass"),
	}, viper.GetString("Env"))

	//web.KafkaLogsHttp = kq.NewKafka(kq.HttpLogs)

	return &ServiceContext{
		Logger:              util.Log(),
		Orm:                 cache.DB,
		Redis:               cache.Redis,
		UserInfoMiddleware:  middleware.NewUserInfoMiddleware(viper.GetString("JWT.Secret")).Handle(),
		RateLimitMiddleware: middleware.NewRateLimitMiddleware(time.Second, 100, 100).Handle(),
	}
}
